package graphicView;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

//import PopupMenuDemo.PopupListener;

/**
 * Team EBE
 * 
 * DiagramUserInterface.java is the graphical user interface based off of Dr.
 * Hutchens' puzzle example, and modified to handle UML diagrams
 * 
 */
public class DiagramUserInterface extends JPanel implements ActionListener {

	private ActiveClassNotifier activeclassNotifier;
	private static final long serialVersionUID = 1L;

	private JFrame frame;
	private GraphicController controller;
	private JTextArea theOutputMessage;

	private Vector<ClassGraphicObject> classesToDraw;

	private LayoutManager Layout;
	private Vector<RelationshipGraphicObject> relationshipsToDraw;
	private JPopupMenu contextMenu;

	MyPanel drawFrame;

	private static final String saveMenuText = "Save UML";
	private static final String openMenuText = "Open UML";
	private static final String insertText = "Insert";
	private static final String classText = "ClassImpl";
	private static final String packageText = "Package";
	private static final String findButtonText = "Find Word";
	private static final String relationText = "RelationshipImpl";
	
	private int popupX, popupY;

	public DiagramUserInterface(GraphicController aController, JFrame aFrame,
			Vector<ClassGraphicObject> cgoIn,
			Vector<RelationshipGraphicObject> rgoIn,
			ActiveClassNotifier activeClassNotifierIn) {

		activeclassNotifier = activeClassNotifierIn;
		classesToDraw = cgoIn;
		relationshipsToDraw = rgoIn;
		frame = aFrame;
		controller = aController;
		JPanel classPanel = setupLayout();
		drawFrame = new MyPanel(classesToDraw, relationshipsToDraw,
				activeclassNotifier);
		activeclassNotifier.addObserver(drawFrame);
		createMenu(frame);
		createPopupMenu(contextMenu);

		frame.getContentPane().add(classPanel, BorderLayout.CENTER);

		frame.getContentPane().add(drawFrame);

		// createMenu(frame);

		// createButton(widgetPanel);
		// addMouseListener(this);
	}

	private void createMenu(JFrame frame2) {

		JMenuItem RelationItem, ClassItem, PackageItem;
		JMenu Insert;

		// create a menu bar
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		// create the File Item on the menu bar
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);

		// create the openPuzzle submenu item and listen for it
		JMenuItem openPuzzle = new JMenuItem(openMenuText);
		openPuzzle.addActionListener(this);
		fileMenu.add(openPuzzle);

		// create the savePuzzle submenu item and listen for it
		JMenuItem savePuzzle = new JMenuItem(saveMenuText);
		savePuzzle.addActionListener(this);
		fileMenu.add(savePuzzle);

		// create the Edit Item on the menu bar
		JMenu editMenu = new JMenu("Edit");
		menuBar.add(editMenu);

		// JMenuItem Insert = new JMenuItem(insertText);
		// Insert.addActionListener(this);
		// editMenu.add(Insert);

		Insert = new JMenu(insertText);
		Insert.setMnemonic(KeyEvent.VK_S);
		Insert.addActionListener(this);
		editMenu.add(Insert);

		PackageItem = new JMenuItem(packageText);
		/*
		 * subMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
		 * ActionEvent.CTRL_MASK));
		 */
		PackageItem.addActionListener(this);
		Insert.add(PackageItem);

		ClassItem = new JMenuItem(classText);
		Insert.add(ClassItem);
		ClassItem.addActionListener(this);

		RelationItem = new JMenuItem(relationText);
		Insert.add(RelationItem);
		RelationItem.addActionListener(this);

		editMenu.add(Insert);

	}

	public void createPopupMenu(JPopupMenu theMenu) {
		JMenuItem menuItem;

		// Create the popup menu.
		theMenu = new JPopupMenu();
		menuItem = new JMenuItem("Insert Class");
		menuItem.addActionListener(this);
		theMenu.add(menuItem);
		menuItem = new JMenuItem("Insert Relationship");
		menuItem.addActionListener(this);
		theMenu.add(menuItem);

		// Add listener to the text area so the popup menu can come up.
		MouseListener popupListener = new PopupListener(theMenu, this);
		drawFrame.addMouseListener(popupListener);
	}
	public void updatePopupLoc(int x, int y){
		popupX = x;
		popupY = y;
	}

	private JPanel setupLayout() {

		JPanel drawField = new JPanel();

		Layout = new BorderLayout();
		setLayout(new BorderLayout());

		drawField.setPreferredSize(new Dimension(1600, 800));
		drawField.setBorder(BorderFactory.createLineBorder(Color.gray, 2));

		// widgetPanel.add(JSBH, "North");

		// JFK.add(JSP);

		return drawField;
	}

	/*
	 * public void paintComponent(Graphics g) { super.paintComponent(g);
	 * //g.drawString("This is my custom Panel!", 10, 20); for
	 * (ClassGraphicObject cgo : classesToDraw) { cgo.drawClass(g); } }
	 */

	@Override
	public void actionPerformed(ActionEvent ae) {

		String command = ae.getActionCommand();
		if (command == findButtonText) {
			// findWord();
		} else if (command == insertText) {

		} else if (command == relationText) {
			controller.openRelation();
		} else if (command == classText) {
			controller.openClass();
		} else if (command == "Insert Class"){
			controller.addClass(popupX, popupY);
			System.out.println("FUUUUUUUCCCCCKKKKKKDKDKDKEFKWKFLKSLEK");
			repaint();
		}
		/*  else if(command == openMenuText){
			controller.openUMLFile();
		}
		  else if(command == saveMenuText){
			controller.saveUMLFile();
		}*/
	}

	public void printMessage(String theMessage) {
		theOutputMessage.setText(theMessage);
	}

	public String obtainFileName(String message) {
		String theFileName = null;
		final JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			try {
				theFileName = file.getCanonicalPath();
			} catch (Exception e) {
				printMessage("Unable to determine file path.");
				theFileName = null;
			}
		}
		return (theFileName);
	}
	
	public String saveFileName(String message){
		String filename = null;
		final JFileChooser jfc = new JFileChooser();
		int returnval = jfc.showSaveDialog(this);
		if(returnval == JFileChooser.APPROVE_OPTION){
			File thefile = jfc.getSelectedFile();
			try {
				filename = thefile.getCanonicalPath();
			} catch (Exception e) {
				printMessage("Unable to determine file path.");
				filename = null;
			}
		}
		
		return (filename);
		
	}

	// borrowed code from the java trail
	// http://download.oracle.com/javase/tutorial/uiswing/examples/components/PopupMenuDemoProject/src/components/PopupMenuDemo.java
	class PopupListener extends MouseAdapter {
		JPopupMenu popup;
		DiagramUserInterface theUserInterface;
		PopupListener(JPopupMenu popupMenu, DiagramUserInterface userInterfaceIn) {
			popup = popupMenu;
			theUserInterface = userInterfaceIn;
		}

		public void mousePressed(MouseEvent e) {
			maybeShowPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			maybeShowPopup(e);
		}

		private void maybeShowPopup(MouseEvent e) {
			if (e.isPopupTrigger()) {
				int x = e.getX();
				int y = e.getY();
				
				popup.show(e.getComponent(), x, y);
				theUserInterface.updatePopupLoc(x, y);
			}
		}
	}

}
